package com.ruoyi.comparison.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.comparison.domain.TaskResult;
import com.ruoyi.comparison.domain.bo.TaskResultBo;
import com.ruoyi.comparison.domain.bo.TaskResultDetailBo;
import com.ruoyi.comparison.domain.vo.TaskResultDetailVo;
import com.ruoyi.comparison.domain.vo.TaskResultVo;
import com.ruoyi.comparison.mapper.TaskResultMapper;
import com.ruoyi.comparison.service.ITaskResultDetailService;
import com.ruoyi.comparison.service.ITaskResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 比对结果Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-08
 */
@RequiredArgsConstructor
@Service
public class TaskResultServiceImpl implements ITaskResultService {

    private final TaskResultMapper baseMapper;
    private final ITaskResultDetailService detailService;

    /**
     * 查询比对结果
     */
    @Override
    public TaskResultVo queryById(Long id){

        return baseMapper.selectVoById(id);
    }

    /**
     * 查询比对结果列表
     */
    @Override
    public TableDataInfo<TaskResultVo> queryPageList(TaskResultBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskResult> lqw = buildQueryWrapper(bo);
        lqw.eq(TaskResult::getCheckId,bo.getCheckId());
        Page<TaskResultVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询比对结果列表
     */
    @Override
    public List<TaskResultVo> queryList(TaskResultBo bo) {
        LambdaQueryWrapper<TaskResult> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaskResult> buildQueryWrapper(TaskResultBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaskResult> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStatus() != null, TaskResult::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getLeftName()), TaskResult::getLeftName, bo.getLeftName());
        lqw.eq(bo.getLeftNum() != null, TaskResult::getLeftNum, bo.getLeftNum());
        lqw.eq(bo.getLeftFileSize() != null, TaskResult::getLeftFileSize, bo.getLeftFileSize());
        lqw.like(StringUtils.isNotBlank(bo.getRightName()), TaskResult::getRightName, bo.getRightName());
        lqw.eq(bo.getRightNum() != null, TaskResult::getRightNum, bo.getRightNum());
        lqw.eq(bo.getRightFileSize() != null, TaskResult::getRightFileSize, bo.getRightFileSize());
        lqw.eq(StringUtils.isNotBlank(bo.getPrefix()), TaskResult::getPrefix, bo.getPrefix());
        return lqw;
    }


    /**
     * 新增比对结果
     */
    @Override
    public Boolean insertByBo(TaskResultBo bo) {
        TaskResult add = BeanUtil.toBean(bo, TaskResult.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改比对结果
     */
    @Override
    public Boolean updateByBo(TaskResultBo bo) {
        TaskResult update = BeanUtil.toBean(bo, TaskResult.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaskResult entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除比对结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insert(TaskResult taskResult) {
        this.baseMapper.insert(taskResult);
    }

    @Override
    public TaskResultVo findById(Long id) {
        LambdaQueryWrapper<TaskResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TaskResult::getCheckId,id);
        TaskResultVo taskResultVo = this.baseMapper.selectVoOne(wrapper);
        return taskResultVo;
    }

    @Override
    public boolean deleteByResultId(List<Long> ids) {
        LambdaQueryWrapper<TaskResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TaskResult::getCheckId,ids);
        return this.baseMapper.delete(wrapper)>0;
    }

    @Override
    public List<TaskResult> selsectByIds(List<Long> ids) {
        LambdaQueryWrapper<TaskResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TaskResult::getCheckId,ids);
        List<TaskResult> taskResults = this.baseMapper.selectList(wrapper);
        return taskResults;
    }
}
